package cn.com.chec.en.controller.front;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelpFrontController {
	protected static final Logger logger = Logger
			.getLogger(HelpFrontController.class);

	@SuppressWarnings("deprecation")
	@RequestMapping(value = { "/help" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView help(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String filePath = request.getRealPath("/") + "static/help/help.pdf";
		File loadFile = new File(filePath);
		if (loadFile.isFile()) {
			String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
			response.setContentType("application/pdf");
			response.setHeader("Location", fileName);
			response.setHeader("Cache-Control", "max-age=1000");
			response.setHeader("Content-Disposition",
					"attachment; filename=help.pdf");
			OutputStream outputStream = response.getOutputStream();
			InputStream inputStream = new FileInputStream(loadFile);
			byte[] buffer = new byte[1024];
			int i = 0;
			while ((i = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, i);
			}
			outputStream.flush();
			outputStream.close();
			inputStream.close();
			return null;
		}
		return null;
	}
}